// pages/custom-tab/sign/sign.js
var app = getApp()
Component({

  lifetimes: {
    attached() {
      // 在组件实例进入页面节点树时执行
      this.getSigLogList();
      wx.setNavigationBarTitle({
        title: '待签'
      })
    },
    detached() {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /* 开启全局样式设置 */
  options: {
    addGlobalClass: true,
  },

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    signLogList: [],
    //当前页面
    current: 1,
    //每页最大数
    size: 8,
    //总页面数
    pages: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {

    toMapPilot: function(res) {
      console.log(res)
      var endLat = res.currentTarget.dataset.endlat
      var endLng = res.currentTarget.dataset.endlng
      var endName = res.currentTarget.dataset.endname
      console.log(endLat)
      wx.navigateTo({
        url: '/pages/map/pilot/pilot?endLat=' + endLat + "&endLng=" + endLng + "&endName=" + endName,
      })
    },

    openQr: function(res) {
      console.log(res)
      //下载
      var signLogId = res.currentTarget.dataset.signLogId
      //构造url
      var signQrCodeUrl = app.host + "/sign/my/qrcode?signLogId=" + signLogId ;
      var signQrCodeFile = []
      //下载
      wx.downloadFile({
        url: signQrCodeUrl,
        success(res) {
          console.log(res)
          if (res.statusCode === 200) {
            signQrCodeFile.push(res.tempFilePath)
            wx.previewImage({
              urls: signQrCodeFile,
            })
          }else{
            wx.showToast({
              title: '加载二维码识别!',
              icon:'none'
            })
          }
        },
        fail(res){
          wx.showToast({
            title: '请求服务器失败!',
            icon: 'none'
          })
        }
      })

    },

    getSigLogList: function() {
      wx.showLoading({
        title: '加载中',
      })
      var that = this
      var signLogList = this.data.signLogList
      wx.request({
        url: app.host + '/signLog/myLog',
        data: {
          status: 1,
          current: that.data.current,
          size: that.data.size
        },
        method: 'get',
        header: app.defaultHeader,
        complete(res) {
          console.log(res)
          wx.hideLoading()
          if (res.statusCode == 200) {
            if (res.data.code == 0) {
              var result = signLogList.concat(res.data.data.records)
              that.setData({
                signLogList: result,
                pages: res.data.data.pages,
                current: res.data.data.current
              })
            } else {
              that.setData({
                signLogList: []
              })
            }
          } else {
            wx.showToast({
              title: "请求服务器失败，请检查网络!",
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    },

  }
})